package org.liang.family_manager.service;

import lombok.RequiredArgsConstructor;
import org.liang.family_manager.dao.po.FamilyAdmin;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.liang.family_manager.dao.repo.FamilyAdminRepository;

import java.util.Collections;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final FamilyAdminRepository familyAdminRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        FamilyAdmin admin = familyAdminRepository.findByUsername(username)
            .orElseThrow(() -> new UsernameNotFoundException("用户不存在"));
        
        return new User(admin.getUsername(), admin.getPassword(),
            Collections.singletonList(new SimpleGrantedAuthority("ROLE_ADMIN")));
    }
}